package com.bw.mallshot;


import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bw.mvvm.entity.ClassFiyEntity;
import com.bw.mallshot.databinding.ActivityMainBinding;
import com.bw.mvvm.arouter.MyARouter;
import com.bw.mvvm.custom.BottomCustom;
import com.bw.mvvm.mvvm.view.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * @created by LiuYuShan on 2021/11/22 15:19.
 * @Describe: MainActivity
 */

public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> {


    private List<Fragment> fragments = new ArrayList<>();

    private Fragment homeFragment, classFiyFragment, shoppingFragment, newsFragment, myFragment;
    private BottomCustom homeCustom, classFiyCustom, shoppingCustom, newsCustom, myCustom;
    Fragment classfiy, home, shopping, my, news;

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public int initVerable() {
        return BR.vm;
    }

    public void initView() {
        classfiy = (Fragment) ARouter.getInstance().build(MyARouter.CLASSFIYFRAGMENT).navigation();
        home = (Fragment) ARouter.getInstance().build(MyARouter.HOMEFRAGMENT).navigation();
        shopping = (Fragment) ARouter.getInstance().build(MyARouter.SHOPPINGFRAGMENT).navigation();
        news = (Fragment) ARouter.getInstance().build(MyARouter.NEWSFRAGMENT).navigation();
        my = (Fragment) ARouter.getInstance().build(MyARouter.MYFRAGMENT).navigation();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        initView();
        homeCustom = findViewById(R.id.my_zy);
        classFiyCustom = findViewById(R.id.my_fl);
        shoppingCustom = findViewById(R.id.my_gwc);
        newsCustom = findViewById(R.id.my_xx);
        myCustom = findViewById(R.id.my_my);
        homeCustom.setConten("首页")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.BLUE)
                .setSelImag(com.bw.mvvm.R.drawable.zytrue)
                .setNomalImag(com.bw.mvvm.R.drawable.zy)
                .setSelSize(5)
                .setNomalSize(3)
                .setLinstenner(mViewModel)
                .refresh();

        classFiyCustom.setConten("分类")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.BLUE)
                .setSelImag(com.bw.mvvm.R.drawable.fltrue)
                .setNomalImag(com.bw.mvvm.R.drawable.flflase)
                .setSelSize(5)
                .setNomalSize(3)
                .setLinstenner(mViewModel)
                .refresh();
        shoppingCustom.setConten("购物车")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.BLUE)
                .setSelImag(com.bw.mvvm.R.drawable.gwcture)
                .setNomalImag(com.bw.mvvm.R.drawable.gec)
                .setSelSize(5)
                .setNomalSize(3)
                .setLinstenner(mViewModel)
                .refresh();
        newsCustom.setConten("消息")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.BLUE)
                .setSelImag(com.bw.mvvm.R.drawable.xxtrue)
                .setNomalImag(com.bw.mvvm.R.drawable.xxfalse)
                .setSelSize(5)
                .setNomalSize(3)
                .setLinstenner(mViewModel)
                .refresh();
        myCustom.setConten("我的")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.BLUE)
                .setSelImag(com.bw.mvvm.R.drawable.mytrue)
                .setNomalImag(com.bw.mvvm.R.drawable.myflase)
                .setSelSize(5)
                .setNomalSize(3)
                .setLinstenner(mViewModel)
                .refresh();

        Fragment fragment = (Fragment) ARouter.getInstance().build(MyARouter.HOMEFRAGMENT).navigation();
        if (fragment != null) {
            homeFragment = fragment;
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.my_fragment, homeFragment).commit();
            fragments.add(homeFragment);
        }
        BarUtils.setStatusBarLightMode(this, true);
        BarUtils.transparentStatusBar(this);
    }

    @Subscribe(sticky = true)
    public void getShopping(List<ClassFiyEntity.DataBean> dataBeans) {
        EventBus.getDefault().post(dataBeans.get(0));
        Fragment fragment = (Fragment) ARouter.getInstance().build(MyARouter.SHOPPINGFRAGMENT).navigation();
        if (fragment != null) {

            shoppingFragment = fragment;
            getSupportFragmentManager()
                    .beginTransaction()
                    .show(shoppingFragment).commitAllowingStateLoss();
        }
    }


    @Subscribe()
    public void getEventBus(String msg) {
        if (!msg.isEmpty()) {
            clearBottonSel();
            hideFragment();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            switch (msg) {
                case "home":
                    if (homeFragment == null) {
                        homeFragment = home;
                        fragments.add(homeFragment);
                        fragmentTransaction.add(R.id.my_fragment, homeFragment);

                    } else {
                        fragmentTransaction.show(homeFragment);
                    }
                    homeCustom.setSelFlag(true);
                    fragmentTransaction.commit();
                    break;
                case "classfiy":
                    if (classFiyFragment == null) {
                        classFiyFragment = classfiy;
                        fragments.add(classFiyFragment);
                        fragmentTransaction.add(R.id.my_fragment, classFiyFragment);

                    } else {
                        fragmentTransaction.show(classFiyFragment);
                    }
                    classFiyCustom.setSelFlag(true);
                    fragmentTransaction.commit();
                    break;
                case "shopping":
                    if (shoppingFragment == null) {
                        shoppingFragment = shopping;
                        fragments.add(shoppingFragment);
                        fragmentTransaction.add(R.id.my_fragment, shoppingFragment);
                    } else {
                        fragmentTransaction.show(shoppingFragment);
                    }
                    shoppingCustom.setSelFlag(true);
                    fragmentTransaction.commitAllowingStateLoss();
                    break;
                case "news":
                    if (newsFragment == null) {
                        newsFragment = news;
                        fragments.add(newsFragment);
                        fragmentTransaction.add(R.id.my_fragment, newsFragment);
                    } else {
                        fragmentTransaction.show(newsFragment);
                    }
                    newsCustom.setSelFlag(true);
                    fragmentTransaction.commit();
                    break;
                case "my":
                    if (myFragment == null) {
                        myFragment = my;
                        fragments.add(myFragment);
                        fragmentTransaction.add(R.id.my_fragment, myFragment);
                    } else {
                        fragmentTransaction.show(myFragment);
                    }
                    myCustom.setSelFlag(true);
                    fragmentTransaction.commit();
                    break;
            }
        }
    }

    private void clearBottonSel() {
        homeCustom.setSelFlag(false);
        myCustom.setSelFlag(false);
        shoppingCustom.setSelFlag(false);
        newsCustom.setSelFlag(false);
        classFiyCustom.setSelFlag(false);
    }

    private void hideFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {
            fragmentTransaction.hide(fragments.get(i));
        }
        fragmentTransaction.commitAllowingStateLoss();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}